package com.example.pclimb.repository;

import android.app.Application;

import androidx.lifecycle.LiveData;

import com.example.pclimb.dao.TravelGuideDao;
import com.example.pclimb.database.PclimbDatabase;
import com.example.pclimb.model.TravelGuide;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 旅游攻略数据仓库，处理旅游攻略相关的所有数据操作
 */
public class TravelGuideRepository {
    private TravelGuideDao travelGuideDao;
    private LiveData<List<TravelGuide>> allTravelGuides;
    private LiveData<List<TravelGuide>> topViewedGuides;
    private LiveData<List<TravelGuide>> topLikedGuides;
    private ExecutorService executorService;

    public TravelGuideRepository(Application application) {
        PclimbDatabase database = PclimbDatabase.getInstance(application);
        travelGuideDao = database.travelGuideDao();
        allTravelGuides = travelGuideDao.getAllTravelGuides();
        topViewedGuides = travelGuideDao.getTopViewedTravelGuides();
        topLikedGuides = travelGuideDao.getTopLikedTravelGuides();
        executorService = Executors.newSingleThreadExecutor();
    }

    // 添加旅游攻略
    public void insert(TravelGuide travelGuide, UserRepository.Callback<Long> callback) {
        executorService.execute(() -> {
            long id = travelGuideDao.insert(travelGuide);
            callback.onResult(id);
        });
    }

    // 更新旅游攻略
    public void update(TravelGuide travelGuide) {
        executorService.execute(() -> {
            travelGuideDao.update(travelGuide);
        });
    }

    // 删除旅游攻略
    public void delete(TravelGuide travelGuide) {
        executorService.execute(() -> {
            travelGuideDao.delete(travelGuide);
        });
    }

    // 获取所有旅游攻略
    public LiveData<List<TravelGuide>> getAllTravelGuides() {
        return allTravelGuides;
    }

    // 获取特定旅游攻略
    public LiveData<TravelGuide> getTravelGuideById(int guideId) {
        return travelGuideDao.getTravelGuideById(guideId);
    }

    // 获取作者的所有旅游攻略
    public LiveData<List<TravelGuide>> getTravelGuidesByAuthor(int authorId) {
        return travelGuideDao.getTravelGuidesByAuthorId(authorId);
    }

    // 按地点搜索旅游攻略
    public LiveData<List<TravelGuide>> getTravelGuidesByLocation(String location) {
        return travelGuideDao.getTravelGuidesByLocation(location);
    }

    // 搜索旅游攻略
    public LiveData<List<TravelGuide>> searchTravelGuides(String keyword) {
        return travelGuideDao.searchTravelGuides(keyword);
    }

    // 增加查看次数
    public void incrementViewCount(int guideId) {
        executorService.execute(() -> {
            travelGuideDao.incrementViewCount(guideId);
        });
    }

    // 增加点赞次数
    public void incrementLikeCount(int guideId) {
        executorService.execute(() -> {
            travelGuideDao.incrementLikeCount(guideId);
        });
    }

    // 获取浏览量最高的攻略
    public LiveData<List<TravelGuide>> getTopViewedGuides() {
        return topViewedGuides;
    }

    // 获取点赞最多的攻略
    public LiveData<List<TravelGuide>> getTopLikedGuides() {
        return topLikedGuides;
    }
}